#!/bin/bash
[% c("var/set_default_env") -%]
[% pc(c('var/compiler'), 'var/setup', { compiler_tarfile => c('input_files_by_name/' _ c('var/compiler')) }) %]
distdir=/var/tmp/dist
builddir=/var/tmp/build/[% project %]
mkdir $distdir/[% project %]
mkdir /var/tmp/build

tar -C $GRADLE_HOME -xf $rootdir/[% c('input_files_by_name/gradle') %]

cat > get-moz-build-date << "EOF"
[% INCLUDE "get-moz-build-date" %]
EOF

[% IF !c("var/fetch_gradle_dependencies") %]
  tar -C $distdir -xf [% c('input_files_by_name/geckoview') %]
  gradle_repo=$rootdir/[% c('input_files_by_name/gradle-dependencies') %]
  # This overwrites the release, beta, and nightly geckoview .aar files in our
  # gradle-dependencies directory to be sure that only the one we provide is
  # getting used when building beta/release code.
  find $gradle_repo -type f -name geckoview*omni*.aar -exec cp -f $distdir/geckoview/geckoview*omni*.aar {} \;
  cp -r $gradle_repo/dl/android/maven2/* $gradle_repo
  cp -r $gradle_repo/maven2/* $gradle_repo
  cp -r $gradle_repo/m2/* $gradle_repo
  # Put the Kotlin/Native compiler at the right place, see:
  # tor-browser-build#40217.
  kotlin_dir=/home/rbm/.konan
  mkdir $kotlin_dir
  find $gradle_repo -type f -name kotlin-native-prebuilt-linux*tar.gz -exec tar -C $kotlin_dir -xaf {} \;
  tar -C $distdir -xf [% c('input_files_by_name/application-services') %]
  # XXX: We could be smarter and just copy over the projects that are needed
  # according to the Gradle dependencies list.
  cp -rf $distdir/application-services/maven/org $gradle_repo
[% END %]
tar -C /var/tmp/build -xf [% project %]-[% c('version') %].tar.gz

cd $builddir-[% c('version') %]

# Avoid call to `getGitHash()`, which will fail b/c it invokes git, which is absent in
# the build container. Instead just inject the commit hash, which we know!
sed -i 's/getGitHash()/\"[% c('abbrev') %]\"/' components/support/base/build.gradle
sed -i 's/tag = getGitHash()/tag = \"[% c('abbrev') %]\"/' publish.gradle

[% IF c("var/fetch_gradle_dependencies") %]
  # XXX: fetching deps for `assembleGeckoBeta -x lint` by using that same target
  # results in some missing dependencies for yet unknown reasons. Thus, we use
  # `assemble` instead for now.
  gradle --debug --no-daemon assemble > gradle.log 2>&1 || ( cat gradle.log; exit 1 )
  $rootdir/gen_gradle_deps_file.sh gradle.log
  mv gradle-dependencies-list.txt '[% dest_dir _ "/" _ c("filename") %]'
[% ELSE %]
  # Prepare Glean dependencies for offline build
  tar -xf $rootdir/[% c('input_files_by_name/glean') %]
  # We need to set `LC_ALL` and `LANG` to something that is not ASCII as encoding
  # otherwise `click` barfs. See: https://click.palletsprojects.com/python3/
  export LC_ALL=C.UTF-8
  export LANG=C.UTF-8
  patch -p1 < $rootdir/mavenLocal.patch

  eval $(perl $rootdir/get-moz-build-date [% pc("fenix", "var/copyright_year") %] [% pc("fenix", "var/torbrowser_version") %])
  if [ -z $MOZ_BUILD_DATE ]
  then
    echo "MOZ_BUILD_DATE is not set"
    exit 1
  fi

  # Increase the maximum memory usage, for when compiling with --disable-install-strip
  gradle_args="--offline --no-daemon -Dmaven.repo.local=$gradle_repo -Dorg.gradle.jvmargs=-Xmx8g"

  gradle $gradle_args assembleGecko -x lint
  gradle $gradle_args publish

  # We only need the archives and .pom files for now.
  find components -regex '.*[% c('var/android_components_version') %].\(aar\|jar\|pom\)' -exec cp --parents {} $distdir \;

  # Package
  cd $distdir
  # Package just the maven directory structure for easier replacement of
  # Mozilla's android-components.
  find components -type d -name maven -exec cp -rf {} [% project %] \;
  [% c('tar', {
        tar_src => [ project ],
        tar_args => '-czf ' _ dest_dir _ '/' _ c('filename'),
    }) %]
[% END %]
